<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\CouponCode;
use App\Models\CouponType;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class CouponCodeController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new CouponCode(), function (Grid $grid) {

            $grid->disableActions();
            $grid->disableCreateButton();
            $grid->quickSearch();
            $grid->model()->where('used_system', '!=', 0);


            $grid->column('id')->sortable();
            $grid->column('card_secret');
            $grid->column('validity_time')->display(function ($validity_time) {
                return ($validity_time / (3600 * 24)) . '月';
            });
            $grid->column('exchange_end_time');
            $grid->column('coupon_type_id')->display(function ($coupon_type_id) {
                return CouponType::find($coupon_type_id)->title;
            });
            $grid->column('use_time');
            $grid->column('used_system')->using([
                0   =>  '',
                1   =>  '积分商城',
                2   =>  'cps'
            ]);
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
        
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new CouponCode(), function (Show $show) {
            $show->field('id');
            $show->field('code');
            $show->field('validity_time');
            $show->field('exchange_end_time');
            $show->field('coupon_type_id');
            $show->field('use_time');
            $show->field('used_system');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CouponCode(), function (Form $form) {
            $form->display('id');
            $form->text('code');
            $form->text('validity_time');
            $form->text('exchange_end_time');
            $form->text('coupon_type_id');
            $form->text('use_time');
            $form->text('used_system');
        
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
